package com.chen.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chen.pojo.Article;
import com.chen.pojo.ArticleType;
import com.chen.pojo.Type;
import com.chen.service.ArticleService;
import com.chen.service.TypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//首页访问
@Controller
public class IndexController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TypeService typeService;

    //进入首页加载
    @RequestMapping(value = "/index/{pageNum}",method = RequestMethod.GET)
    public String home(@PathVariable("pageNum") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, HttpServletResponse response, HttpServletRequest request
    , Model model) {

        //Integer type_id =Integer.parseInt(request.getParameter("type_id"));
        //JSONObject JSON = new JSONObject();
        //博客列表
        List<ArticleType> articles = articleService.allBlog(pageNum,pageSize);
        PageInfo<ArticleType> pageInfo = new PageInfo<>(articles);

        //放入Model变量
        model.addAttribute("articles",articles)
        //最近的
                .addAttribute("recentList",articleService.getRecent())
                .addAttribute("pageInfo",pageInfo)
                .addAttribute("status",1);
        //类型列表
        List<Type> types = typeService.allType();
        request.getSession().getServletContext().setAttribute("types",types);

        return "article/home";

    }


}
